// pages/apologies/apologies.js
import {request, requestWrap} from "../../request/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dateRange: '',
        dataList: '',
        isShow: false,
        startdate: '',
        enddate: '',
        start: '',
        end: ''
    },
    id: '',
    name: '',
    student: '',
    startdate: '',
    enddate: '',
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.student = wx.getStorageSync('student')
        this.getCourse()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    handleInput (e) {
      let name = e.currentTarget.dataset.name
      const {value} = e.detail
      this[name] = value
    },
    async getCourse () {
        let data = {}
        data.student_id = this.student.id
        if (this.name) {
          data.name = this.name
        }
        let res = requestWrap(await request({
            url:"api/wxapp/student/studyList",
            method:'GET',
            data
        }))
        if (res && res.code == 1) {
          if (res.data && res.data.length > 0) {
            let dataList = res.data.map(data => {
                let arr = []
                if (data.monday == 1) {
                    arr.push('周一')
                }
                if (data.tuesday == 1) {
                    arr.push('周二')
                }
                if (data.wednesday == 1) {
                    arr.push('周三')
                }
                if (data.thursday == 1) {
                    arr.push('周四')
                }
                if (data.friday == 1) {
                    arr.push('周五')
                }
                if (data.saturday == 1) {
                    arr.push('周六')
                }
                if (data.sunday == 1) {
                    arr.push('周日')
                }
                data.school_week = arr
                if (data.start_time) {
                    data.start_time = data.start_time.substr(0, 10)
                }
                if (data.end_time) {
                    data.end_time = data.end_time.substr(0, 10)
                }
                return data
            })
            this.setData({
              dataList
            })
          } else {
            this.setData({
              dataList: ''
            })
          }
        }
    },
    hidePopup () {
        this.setData({
            isShow: false
        })
    },
    bindDateChange (e) {
        let {name} = e.currentTarget.dataset
        let {value} = e.detail
        let date = value.replace('-', '/')
        let timestamp = Date.parse(new Date(date)) / 1000
        if (name == 'enddate') {
            if (this.startdate && timestamp < this.startdate) {
                wx.showToast({
                  title: '结束日期必须大于等于开始日期',
                  icon: 'none',
                  duration: 2000
                })
                return
            }
        } else {
            if (this.enddate && timestamp > this.enddate) {
                wx.showToast({
                  title: '开始日期必须小于等于结束日期',
                  icon: 'none',
                  duration: 2000
                })
                return
            }
        }
        this.setData({
            [name]: value
        })
        this[name] = timestamp
    },
    onApologies (e) {
        let index = e.currentTarget.dataset.index
        let itemData = this.data.dataList[index]
        this.setData({
            start: itemData.start_time,
            end: itemData.end_time
        })
        this.setData({
            isShow: true
        })
        this.id = itemData.id
    },
    async onAsk () {
        if (!this.startdate || !this.enddate) {
            wx.showToast({
                title: '请选择开始结束日期',
                icon: 'none'
            })
            return
        }
        let data = {}
        data.student_id = this.student.id
        data.course_id = this.id
        data.start_time = this.startdate
        data.end_time = this.enddate
        // console.log(data)
        // return
        let res = requestWrap(await request({
            url:"api/wxapp/student/isAsk",
            method:'GET',
            data
        }))
        if (res && res.code == 1) {
            this.getCourse()
            this.setData({
                startdate: '',
                enddate: '',
                isShow: false
            })
            setTimeout(() => {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            }, 50)
        } else {
            wx.showToast({
                title: res.msg,
                icon: 'none'
            })
        }
    }
})